package com.hankol.community.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.baselibrary.BaseConstant;
import com.baselibrary.bean.FollowSuccBean;
import com.baselibrary.net.APIResponse;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ARouterUtils;
import com.baselibrary.utils.DoubleClickHelper;
import com.baselibrary.utils.GlideUtils;
import com.baselibrary.utils.SpanUtils;
import com.baselibrary.utils.TimeUtil;
import com.baselibrary.view.VipHeardImgView;
import com.hankol.message.HKIMUtils;
import com.hankol.community.R;
import com.hankol.community.bean.RecommendUserBean;
import com.hankol.community.bean.Timeline;
import com.hankol.community.viewmodel.CommunityViewModel;

import java.util.HashMap;
import java.util.List;

/**
 * @author:hanke 2022/4/28
 * desc:
 */
public class CardPagerAdapter extends PagerAdapter {

    List<RecommendUserBean> mPagerViewList;
    Context mContext;
    ClickCallback clickCallback;
    CommunityViewModel communityViewModel;

    public CardPagerAdapter(Context context, CommunityViewModel viewModel) {
        mContext = context;
        communityViewModel = viewModel;
    }

    public void setData(List<RecommendUserBean> pagerViewList) {
        mPagerViewList = pagerViewList;
    }

    public void setCallback(ClickCallback callback) {
        clickCallback = callback;
    }

    @Override
    public int getCount() {
        return mPagerViewList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_recommend_users, null);
        TextView tvTip = view.findViewById(R.id.tv_recommend_tip);
        VipHeardImgView civAvatar = view.findViewById(R.id.circle_attentive_head);
        TextView userName = view.findViewById(R.id.tv_recommend_name);
        RelativeLayout covers = view.findViewById(R.id.rl_locus_covers);
        ImageView coverOne = view.findViewById(R.id.iv_cover_one);
        ImageView coverTwo = view.findViewById(R.id.iv_cover_two);
        ImageView coverThree = view.findViewById(R.id.iv_cover_three);
        TextView tvFocus = view.findViewById(R.id.tv_focus);
        ImageView ivClose = view.findViewById(R.id.iv_recommend_close);
        RecommendUserBean item = mPagerViewList.get(position);
        civAvatar.setData(item.getIcon(), item.isFans() == 1);
        userName.setText(SpanUtils.appendEllipsis(item.getName(), 6));
        if (item.getTimelineList().size() == 0) {
            covers.setVisibility(View.INVISIBLE);
            tvTip.setText(item.getDesc());
        } else {
            covers.setVisibility(View.VISIBLE);
            setDynamicCover(coverOne, coverTwo, coverThree, item);
            String dayAround = TimeUtil.getDayAround(item.getTimelineList().get(0).getCtime());
            if (TextUtils.isEmpty(dayAround)) {
                tvTip.setText(item.getDesc());
            } else {
                tvTip.setText(dayAround);
            }
        }
        ivClose.setOnClickListener(v -> {
            if (!DoubleClickHelper.isOnDoubleClick()) {
                clickCallback.onClickClose(mPagerViewList.get(position).getId(), position);
            }
        });
        civAvatar.setOnClickListener(view1 -> jumpUserInfo(item.getId()));
        userName.setOnClickListener(view12 -> jumpUserInfo(item.getId()));
        covers.setOnClickListener(view12 -> jumpUserInfo(item.getId()));
        tvFocus.setSelected(mPagerViewList.get(position).getHasFocus());
        tvFocus.setText(mPagerViewList.get(position).getHasFocus() ? "已关注" : "关注");
        tvFocus.setOnClickListener(v -> {
            if (!DoubleClickHelper.isOnDoubleClick()) {
                boolean doFocus = !mPagerViewList.get(position).getHasFocus();
                communityViewModel.followAction2((Activity) mContext, doFocus, mPagerViewList.get(position).getId(), new APIResponse<FollowSuccBean>(this) {
                    @Override
                    public void onSuccess(FollowSuccBean result, String json) {
                        mPagerViewList.get(position).setHasFocus(doFocus);
                        tvFocus.setSelected(mPagerViewList.get(position).getHasFocus());
                        tvFocus.setText(mPagerViewList.get(position).getHasFocus() ? "已关注" : "关注");
                        clickCallback.onClickFocus(doFocus, position);
                        if (mPagerViewList.get(position).getHasFocus()) {//关注成功--发送消息
                            HKIMUtils.followSendMsg(mPagerViewList.get(position).getId(), result.getFriendState());
                        }
                    }
                });
            }
        });
        container.addView(view);
        return view;
    }

    private void setDynamicCover(ImageView coverOne, ImageView coverTwo, ImageView coverThree, RecommendUserBean item) {
        List<Timeline> timelineList = item.getTimelineList();
        if (timelineList.size() == 1) {
            coverOne.setVisibility(View.VISIBLE);
            coverTwo.setVisibility(View.GONE);
            coverThree.setVisibility(View.GONE);
            GlideUtils.loadImg_avatar(mContext, timelineList.get(0).getCover(), coverOne);
        } else if (timelineList.size() == 2) {
            coverOne.setVisibility(View.VISIBLE);
            coverTwo.setVisibility(View.VISIBLE);
            coverThree.setVisibility(View.GONE);
            GlideUtils.loadImg_avatar(mContext, timelineList.get(0).getCover(), coverOne);
            GlideUtils.loadImg_avatar(mContext, timelineList.get(1).getCover(), coverTwo);
        } else if (timelineList.size() == 3) {
            coverOne.setVisibility(View.VISIBLE);
            coverTwo.setVisibility(View.VISIBLE);
            coverThree.setVisibility(View.VISIBLE);
            GlideUtils.loadImg_avatar(mContext, timelineList.get(0).getCover(), coverOne);
            GlideUtils.loadImg_avatar(mContext, timelineList.get(1).getCover(), coverTwo);
            GlideUtils.loadImg_avatar(mContext, timelineList.get(2).getCover(), coverThree);
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public float getPageWidth(int position) {
        return 1f;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    public interface ClickCallback {
        void onClickClose(int userId, int position);

        void onClickFocus(boolean doFocus, int position);
    }

    private void jumpUserInfo(long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(BaseConstant.USER_ID, String.valueOf(userId));
        ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap);
    }
}
